﻿Public Class Form6GongWeiOut
    Public usr As String
    Dim objGt As New clsGt
    Dim objGtResultDetails As New clsGtResultDetails
    Dim objGtNgReasonDetails As New clsGtNgReasonDetails
    Dim objGtProcedure As New clsGtProcedure
    Dim objGtProcedureDetails As New clsGtProcedureDetails
    Dim dt As DataTable
    Private Sub Form6GongWeiOut_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setData()
    End Sub

    Public Sub setData()
        dt = objGt.getCurrentGt("0", "2", "3")
        dt.Columns.Add("oper_type_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Public Sub setDgvOperData()
        If dgv.SelectedRows.Count > 0 Then
            Dim dtProcedure As DataTable = objGtProcedure.selectGtProcedure(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "3")
            DataGridView3.AutoGenerateColumns = False
            DataGridView3.DataSource = dtProcedure
            Dim dtOper As DataTable = objGtProcedureDetails.getGtProcedureDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "3")
            dgvOper.AutoGenerateColumns = False
            dgvOper.DataSource = dtOper
        End If
    End Sub

    Public Sub SetDgvResultData()
        Dim dtResult As DataTable = objGtResultDetails.getGtResultDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "3")
        DataGridView2.AutoGenerateColumns = False
        DataGridView2.DataSource = dtResult
    End Sub

    Public Sub setDgvNgOperData()
        Dim dtNgOper As DataTable = objGtNgReasonDetails.getGtNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "3")
        dgvNgOper.AutoGenerateColumns = False
        dgvNgOper.DataSource = dtNgOper
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count > 0 Then
            setDgvOperData()
            SetDgvResultData()
            setDgvNgOperData()
        End If
    End Sub

    Private Sub btnNg_Click(sender As Object, e As EventArgs) Handles btnNg.Click
        If dgvOper.SelectedRows.Count > 0 Then
            Dim formNgOper As New FormNgOper()
            'formNgOper.usr = usr
            formNgOper.source = "6gw"
            formNgOper.ty = "0"
            formNgOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            formNgOper.current_procedure_No = 3
            formNgOper.ng_num_all = DataGridView2.SelectedRows(0).Cells("Co_ng_num").Value.ToString()
            formNgOper.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            formNgOper.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If dgvNgOper.SelectedRows.Count > 0 Then
            If dgvNgOper.SelectedRows(0).Cells("Colu_validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNgOper.SelectedRows(0).Cells("Colu_procedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_b_gt_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNgOper.SelectedRows(0).Cells("Colu_ng_num").Value.ToString()
                    Dim rtn As Integer = objGtNgReasonDetails.updateValidity(sys_id, ng_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        SetDgvResultData()
                        setDgvNgOperData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click
        If dgv.SelectedRows.Count > 0 Then
            If MsgBox("确认完工吗？", vbYesNo, "提示") = vbYes Then
                Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                Dim rtn As Integer = objGt.save("3", sys_id)
                If rtn <> -2 Then
                    MsgBox("完工成功")
                    setData()
                    setDgvOperData()
                    SetDgvResultData()
                    setDgvNgOperData()
                Else
                    MsgBox("完工失败")
                End If
            End If
        End If
    End Sub
End Class